#include "cppTutorGlobals.h"
#include "cppTutorPointers.h"

#include <iostream>
#include <stdio.h>

using namespace std;

CcppTutorPointers :: CcppTutorPointers () : m_cX ('S'), m_nValue (50)
{
}

CcppTutorPointers :: ~CcppTutorPointers ()
{
}

void CcppTutorPointers :: m_VariousTypes ()
{
    //Pointer npPtrVariable which is storing memory address of a int type variable:
    int *npPtrVariable= &m_nValue;
    cout << "npPtrVariable = " << *npPtrVariable << endl;
    
    //Pointer npPtrArray which is storing memory address of an array:
    int (*npPtrArray)[20] = &m_arr;

    //Pointer npPtrFunction which is storing memory address of a function:
    //char(*npPtrFunction)() = &m_Display;
    //char(CcppTutorPointers :: *npPtrFunction)() = &m_Display;
}

char CcppTutorPointers :: m_Display ()
{
    cout << "ENTER : CcppTutorPointers :: m_Display ()" << endl;
    cout << "EXIT  : CcppTutorPointers :: m_Display ()" << endl;
    return m_cX;
}

void CcppTutorPointers:: m_Test ()
{
    cout << "ENTER : CcppTutorPointers :: m_Test ()" << endl;
    int i = 0, j = 0;
    int arr1[5] = {1, 2, 3, 4, 5};
    int *arr[5];

    for (; j<5; j++)
    {
        arr[j] = &arr1[j];
        printf ("%s\n", *arr[j]);
    }

    //Pointer to array of string
    char *array[4] = {"c","c++","java","sql"};
    char *(*ptr)[4] = &array;
    for (; i<4; i++)
    {
       printf ("%s \n", (*ptr)[i]);
    }

    cout << "EXIT  : CcppTutorPointers :: m_Test ()" << endl;
}

void CcppTutorPointers :: m_pointerArithmetic ()
{
	int a[10];
	for(int i = 0; i < 10; i++)
	{
		a[i] = i; // Give it index values
	}
	int* ip = a;
	P(*ip);
	P(*++ip);
	P(*(ip + 5));
	int* ip2 = ip + 5;
	P(*ip2);
	P(*(ip2 - 4));
	P(*--ip2);
	P(ip2 - ip); // Yields number of elements
}
